package run.siyuan.manag.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import run.siyuan.common.entity.Result;
import run.siyuan.common.exception.ExceptionProperties;
import run.siyuan.manag.annotation.OperLog;
import run.siyuan.manag.entity.SysUser;
import run.siyuan.manag.service.SysUserService;
import run.siyuan.manag.util.SecurityUtil;

/**
 * @author wzq
 * @description: TODO
 * @date 2021/6/4 4:02 下午
 */
@Slf4j
@RestController
@RequestMapping("/manag")
public class UserController {

    @Autowired
    private SysUserService userService;
    @Autowired
    private ExceptionProperties exceptionProperties;


    /**
     * 根据 token 获取用户信息
     * @param
     * @return {@link run.siyuan.manag.entity.SysUser}
     */
    @OperLog(description = "获取用户信息")
    @GetMapping("/userInfo")
    public Result userInfo() {
        // 获取令牌并根据令牌获取登入信息
        String username = SecurityUtil.getUsername();
        log.info("用户名：{}", username);
        SysUser user = userService.lambdaQuery().eq(SysUser::getName, username).one();
        if (ObjectUtil.isEmpty(user)){
            return Result.error(50006, exceptionProperties.getCodeName("50006"));
        }

        return Result.success().data(user);
    }


}
